home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / WordPress 1.5.1.dmg / wordpress / wp-includes / comment-functions.php < prev    next >
Encoding:
PHP Script  |  2005-04-26  |  21.0 KB  |  677 lines

  1. <?php
  2.  
  3. // Template functions
  4.  
  5. function comments_template( $file = '/comments.php' ) {
  6.     global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity;
  7.  
  8.     if ( is_single() || is_page() || $withcomments ) :
  9.         $req = get_settings('require_name_email');
  10.         $comment_author = isset($_COOKIE['comment_author_'.COOKIEHASH]) ? trim(stripslashes($_COOKIE['comment_author_'.COOKIEHASH])) : '';
  11.         $comment_author_email = isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ? trim(stripslashes($_COOKIE['comment_author_email_'.COOKIEHASH])) : '';
  12.         $comment_author_url = isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ? trim(stripslashes($_COOKIE['comment_author_url_'.COOKIEHASH])) : '';
  13.     if ( empty($comment_author) ) {
  14.         $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND comment_approved = '1' ORDER BY comment_date");
  15.     } else {
  16.         $author_db = addslashes($comment_author);
  17.         $email_db  = addslashes($comment_author_email);
  18.         $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND ( comment_approved = '1' OR ( comment_author = '$author_db' AND comment_author_email = '$email_db' AND comment_approved = '0' ) ) ORDER BY comment_date");
  19.     }
  20.  
  21.     get_currentuserinfo();
  22.  
  23.     define('COMMENTS_TEMPLATE', true);
  24.     $include = apply_filters('comments_template', TEMPLATEPATH . $file );
  25.     if ( file_exists( $include ) )
  26.         require( $include );
  27.     else
  28.         require( ABSPATH . 'wp-content/themes/default/comments.php');
  29.  
  30.     endif;
  31. }
  32.  
  33. function clean_url( $url ) {
  34.     if ('' == $url) return $url;
  35.     $url = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $url);
  36.     $url = str_replace(';//', '://', $url);
  37.     $url = (!strstr($url, '://')) ? 'http://'.$url : $url;
  38.     $url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&$1', $url);
  39.     return $url;
  40. }
  41.  
  42. function get_comments_number( $comment_id ) {
  43.     global $wpdb, $comment_count_cache;
  44.     $comment_id = (int) $comment_id;
  45.     if (!isset($comment_count_cache[$comment_id]))
  46.         $comment_count_cache[$comment_id] =  $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$comment_id' AND comment_approved = '1'");
  47.     
  48.     return apply_filters('get_comments_number', $comment_count_cache[$comment_id]);
  49. }
  50.  
  51. function comments_number( $zero = 'No Comments', $one = '1 Comment', $more = '% Comments', $number = '' ) {
  52.     global $id, $comment;
  53.     $number = get_comments_number( $id );
  54.     if ($number == 0) {
  55.         $blah = $zero;
  56.     } elseif ($number == 1) {
  57.         $blah = $one;
  58.     } elseif ($number  > 1) {
  59.         $blah = str_replace('%', $number, $more);
  60.     }
  61.     echo apply_filters('comments_number', $blah);
  62. }
  63.  
  64. function get_comments_link() {
  65.     return get_permalink() . '#comments';
  66. }
  67.  
  68. function get_comment_link() {
  69.     global $comment;
  70.     return get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID;
  71. }
  72.  
  73. function comments_link( $file = '', $echo = true ) {
  74.     echo get_comments_link();
  75. }
  76.  
  77. function comments_popup_script($width=400, $height=400, $file='') {
  78.     global $wpcommentspopupfile, $wptrackbackpopupfile, $wppingbackpopupfile, $wpcommentsjavascript;
  79.  
  80.         if (empty ($file)) {
  81.             $wpcommentspopupfile = '';  // Use the index.
  82.         } else {
  83.             $wpcommentspopupfile = $file;
  84.         }
  85.  
  86.     $wpcommentsjavascript = 1;
  87.     $javascript = "<script type='text/javascript'>\nfunction wpopen (macagna) {\n    window.open(macagna, '_blank', 'width=$width,height=$height,scrollbars=yes,status=yes');\n}\n</script>\n";
  88.     echo $javascript;
  89. }
  90.  
  91. function comments_popup_link($zero='No Comments', $one='1 Comment', $more='% Comments', $CSSclass='', $none='Comments Off') {
  92.     global $id, $wpcommentspopupfile, $wpcommentsjavascript, $post, $wpdb;
  93.     global $comment_count_cache;
  94.  
  95.     if (! is_single() && ! is_page()) {
  96.     if ( !isset($comment_count_cache[$id]))
  97.             $comment_count_cache[$id] = $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = $id AND comment_approved = '1';");
  98.  
  99.         $number = $comment_count_cache[$id];
  100.  
  101.     if (0 == $number && 'closed' == $post->comment_status && 'closed' == $post->ping_status) {
  102.         echo $none;
  103.         return;
  104.     } else {
  105.         if (!empty($post->post_password)) { // if there's a password
  106.             if ($_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password) {  // and it doesn't match the cookie
  107.                 echo('Enter your password to view comments');
  108.                 return;
  109.             }
  110.         }
  111.         echo '<a href="';
  112.         if ($wpcommentsjavascript) {
  113.                     if ( empty($wpcommentspopupfile) )
  114.                         $home = get_settings('home');
  115.                     else
  116.                         $home = get_settings('siteurl');
  117.                     echo $home . '/' . $wpcommentspopupfile.'?comments_popup='.$id;
  118.                     echo '" onclick="wpopen(this.href); return false"';
  119.         } else {
  120.             // if comments_popup_script() is not in the template, display simple comment link
  121.             comments_link();
  122.             echo '"';
  123.         }
  124.         if (!empty($CSSclass)) {
  125.             echo ' class="'.$CSSclass.'"';
  126.         }
  127.         echo '>';
  128.         comments_number($zero, $one, $more, $number);
  129.         echo '</a>';
  130.     }
  131.     }
  132. }
  133.  
  134. function get_comment_ID() {
  135.     global $comment;
  136.     return apply_filters('get_comment_ID', $comment->comment_ID);
  137. }
  138.  
  139. function comment_ID() {
  140.     echo get_comment_ID();
  141. }
  142.  
  143. function get_comment_author() {
  144.     global $comment;
  145.     if ( empty($comment->comment_author) )
  146.         $author = 'Anonymous';
  147.     else
  148.         $author = $comment->comment_author;
  149.     return apply_filters('get_comment_author', $author);
  150. }
  151.  
  152. function comment_author() {
  153.     $author = apply_filters('comment_author', get_comment_author() );
  154.     echo $author;
  155. }
  156.  
  157. function get_comment_author_email() {
  158.     global $comment;
  159.     return apply_filters('get_comment_author_email', $comment->comment_author_email);    
  160. }
  161.  
  162. function comment_author_email() {
  163.     echo apply_filters('author_email', get_comment_author_email() );
  164. }
  165.  
  166. function get_comment_author_link() {
  167.     global $comment;
  168.     $url    = get_comment_author_url();
  169.     $author = get_comment_author();
  170.  
  171.     if ( empty( $url ) )
  172.         $return = $author;
  173.     else
  174.         $return = "<a href='$url' rel='external nofollow'>$author</a>";
  175.     return apply_filters('get_comment_author_link', $return);
  176. }
  177.  
  178. function comment_author_link() {
  179.     echo get_comment_author_link();
  180. }
  181.  
  182. function get_comment_type() {
  183.     global $comment;
  184.  
  185.     if ( '' == $comment->comment_type )
  186.         $comment->comment_type = 'comment';
  187.  
  188.     return apply_filters('get_comment_type', $comment->comment_type);
  189. }
  190.  
  191. function comment_type($commenttxt = 'Comment', $trackbacktxt = 'Trackback', $pingbacktxt = 'Pingback') {
  192.     $type = get_comment_type();
  193.     switch( $type ) {
  194.         case 'trackback' :
  195.             echo $trackbacktxt;
  196.             break;
  197.         case 'pingback' :
  198.             echo $pingbacktxt;
  199.             break;
  200.         default :
  201.             echo $commenttxt;
  202.     }
  203. }
  204.  
  205. function get_comment_author_url() {
  206.     global $comment;
  207.     return apply_filters('get_comment_author_url', $comment->comment_author_url);
  208. }
  209.  
  210. function comment_author_url() {
  211.     echo apply_filters('comment_url', get_comment_author_url());
  212. }
  213.  
  214. function comment_author_email_link($linktext='', $before='', $after='') {
  215.     global $comment;
  216.     $email = apply_filters('comment_email', $comment->comment_author_email);
  217.     if ((!empty($email)) && ($email != '@')) {
  218.     $display = ($linktext != '') ? $linktext : $email;
  219.         echo $before;
  220.         echo "<a href='mailto:$email'>$display</a>";
  221.         echo $after;
  222.     }
  223. }
  224.  
  225. function get_comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
  226.     global $comment;
  227.     $url = get_comment_author_url();
  228.     $display = ($linktext != '') ? $linktext : $url;
  229.     $return = "$before<a href='$url' rel='external'>$display</a>$after";
  230.     return apply_filters('get_comment_author_url_link', $return);
  231. }
  232.  
  233. function comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
  234.     echo get_comment_author_url_link( $linktext, $before, $after );
  235. }
  236.  
  237. function get_comment_author_IP() {
  238.     global $comment;
  239.     return apply_filters('get_comment_author_IP', $comment->comment_author_IP);
  240. }
  241.  
  242. function comment_author_IP() {
  243.     echo get_comment_author_IP();
  244. }
  245.  
  246. function get_comment_text() {
  247.     global $comment;
  248.     return apply_filters('get_comment_text', $comment->comment_content);
  249. }
  250.  
  251. function comment_text() {
  252.     echo apply_filters('comment_text', get_comment_text() );
  253. }
  254.  
  255. function get_comment_excerpt() {
  256.     global $comment;
  257.     $comment_text = strip_tags($comment->comment_content);
  258.     $blah = explode(' ', $comment_text);
  259.     if (count($blah) > 20) {
  260.         $k = 20;
  261.         $use_dotdotdot = 1;
  262.     } else {
  263.         $k = count($blah);
  264.         $use_dotdotdot = 0;
  265.     }
  266.     $excerpt = '';
  267.     for ($i=0; $i<$k; $i++) {
  268.         $excerpt .= $blah[$i] . ' ';
  269.     }
  270.     $excerpt .= ($use_dotdotdot) ? '...' : '';
  271.     return apply_filters('get_comment_excerpt', $excerpt);
  272. }
  273.  
  274. function comment_excerpt() {
  275.     echo apply_filters('comment_excerpt', get_comment_excerpt() );
  276. }
  277.  
  278. function get_comment_date( $d = '' ) {
  279.     global $comment;
  280.     if ( '' == $d )
  281.         $date = mysql2date( get_settings('date_format'), $comment->comment_date);
  282.     else
  283.         $date = mysql2date($d, $comment->comment_date);
  284.     return apply_filters('get_comment_date', $date);
  285. }
  286.  
  287. function comment_date( $d = '' ) {
  288.     echo get_comment_date( $d );
  289. }
  290.  
  291. function get_comment_time( $d = '', $gmt = false ) {
  292.     global $comment;
  293.     $comment_date = $gmt? $comment->comment_date_gmt : $comment->comment_date;
  294.     if ( '' == $d )
  295.         $date = mysql2date(get_settings('time_format'), $comment_date);
  296.     else
  297.         $date = mysql2date($d, $comment_date);
  298.     return apply_filters('get_comment_time', $date);
  299. }
  300.  
  301. function comment_time( $d = '' ) {
  302.     echo get_comment_time($d);
  303. }
  304.  
  305. function get_trackback_url() {
  306.     global $id;
  307.     $tb_url = get_settings('siteurl') . '/wp-trackback.php?p=' . $id;
  308.  
  309.     if ( '' != get_settings('permalink_structure') )
  310.         $tb_url = trailingslashit(get_permalink()) . 'trackback/';
  311.  
  312.     return $tb_url;
  313. }
  314. function trackback_url( $display = true ) {
  315.     if ( $display)
  316.         echo get_trackback_url();
  317.     else
  318.         return get_trackback_url();
  319. }
  320.  
  321. function trackback_rdf($timezone = 0) {
  322.     global $id;
  323.     if (!stristr($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator')) {
  324.     echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
  325.         xmlns:dc="http://purl.org/dc/elements/1.1/"
  326.         xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  327.         <rdf:Description rdf:about="';
  328.     the_permalink();
  329.     echo '"'."\n";
  330.     echo '    dc:identifier="';
  331.     the_permalink();
  332.     echo '"'."\n";
  333.     echo '    dc:title="'.str_replace('--', '--', wptexturize(strip_tags(get_the_title()))).'"'."\n";
  334.     echo '    trackback:ping="'.trackback_url(0).'"'." />\n";
  335.     echo '</rdf:RDF>';
  336.     }
  337. }
  338.  
  339. function comments_open() {
  340.     global $post;
  341.     if ( 'open' == $post->comment_status )
  342.         return true;
  343.     else
  344.         return false;
  345. }
  346.  
  347. function pings_open() {
  348.     global $post;
  349.     if ( 'open' == $post->ping_status ) 
  350.         return true;
  351.     else
  352.         return false;
  353. }
  354.  
  355. // Non-template functions
  356.  
  357. function get_lastcommentmodified($timezone = 'server') {
  358.     global $tablecomments, $cache_lastcommentmodified, $pagenow, $wpdb;
  359.     $add_seconds_blog = get_settings('gmt_offset') * 3600;
  360.     $add_seconds_server = date('Z');
  361.     $now = current_time('mysql', 1);
  362.     if ( !isset($cache_lastcommentmodified[$timezone]) ) {
  363.         switch(strtolower($timezone)) {
  364.             case 'gmt':
  365.                 $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $tablecomments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1");
  366.                 break;
  367.             case 'blog':
  368.                 $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $tablecomments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1");
  369.                 break;
  370.             case 'server':
  371.                 $lastcommentmodified = $wpdb->get_var("SELECT DATE_ADD(comment_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $tablecomments WHERE comment_date_gmt <= '$now' ORDER BY comment_date_gmt DESC LIMIT 1");
  372.                 break;
  373.         }
  374.         $cache_lastcommentmodified[$timezone] = $lastcommentmodified;
  375.     } else {
  376.         $lastcommentmodified = $cache_lastcommentmodified[$timezone];
  377.     }
  378.     return $lastcommentmodified;
  379. }
  380.  
  381. function get_commentdata( $comment_ID, $no_cache = 0, $include_unapproved = false ) { // less flexible, but saves DB queries
  382.     global $postc, $id, $commentdata, $wpdb;
  383.     if ($no_cache) {
  384.         $query = "SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_ID'";
  385.         if (false == $include_unapproved) {
  386.             $query .= " AND comment_approved = '1'";
  387.         }
  388.             $myrow = $wpdb->get_row($query, ARRAY_A);
  389.     } else {
  390.         $myrow['comment_ID'] = $postc->comment_ID;
  391.         $myrow['comment_post_ID'] = $postc->comment_post_ID;
  392.         $myrow['comment_author'] = $postc->comment_author;
  393.         $myrow['comment_author_email'] = $postc->comment_author_email;
  394.         $myrow['comment_author_url'] = $postc->comment_author_url;
  395.         $myrow['comment_author_IP'] = $postc->comment_author_IP;
  396.         $myrow['comment_date'] = $postc->comment_date;
  397.         $myrow['comment_content'] = $postc->comment_content;
  398.         $myrow['comment_karma'] = $postc->comment_karma;
  399.         $myrow['comment_approved'] = $postc->comment_approved;
  400.         $myrow['comment_type'] = $postc->comment_type;
  401.     }
  402.     return $myrow;
  403. }
  404.  
  405. function pingback($content, $post_ID) {
  406.     global $wp_version, $wpdb;
  407.     include_once (ABSPATH . WPINC . '/class-IXR.php');
  408.  
  409.     // original code by Mort (http://mort.mine.nu:8080)
  410.     $log = debug_fopen(ABSPATH . '/pingback.log', 'a');
  411.     $post_links = array();
  412.     debug_fwrite($log, 'BEGIN '.date('YmdHis', time())."\n");
  413.  
  414.     $pung = get_pung($post_ID);
  415.  
  416.     // Variables
  417.     $ltrs = '\w';
  418.     $gunk = '/#~:.?+=&%@!\-';
  419.     $punc = '.:?\-';
  420.     $any = $ltrs . $gunk . $punc;
  421.  
  422.     // Step 1
  423.     // Parsing the post, external links (if any) are stored in the $post_links array
  424.     // This regexp comes straight from phpfreaks.com
  425.     // http://www.phpfreaks.com/quickcode/Extract_All_URLs_on_a_Page/15.php
  426.     preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
  427.  
  428.     // Debug
  429.     debug_fwrite($log, 'Post contents:');
  430.     debug_fwrite($log, $content."\n");
  431.     
  432.     // Step 2.
  433.     // Walking thru the links array
  434.     // first we get rid of links pointing to sites, not to specific files
  435.     // Example:
  436.     // http://dummy-weblog.org
  437.     // http://dummy-weblog.org/
  438.     // http://dummy-weblog.org/post.php
  439.     // We don't wanna ping first and second types, even if they have a valid <link/>
  440.  
  441.     foreach($post_links_temp[0] as $link_test) :
  442.         if ( !in_array($link_test, $pung) ) : // If we haven't pung it already
  443.             $test = parse_url($link_test);
  444.             if (isset($test['query']))
  445.                 $post_links[] = $link_test;
  446.             elseif(($test['path'] != '/') && ($test['path'] != ''))
  447.                 $post_links[] = $link_test;
  448.         endif;
  449.     endforeach;
  450.  
  451.     foreach ($post_links as $pagelinkedto){
  452.         debug_fwrite($log, "Processing -- $pagelinkedto\n");
  453.         $pingback_server_url = discover_pingback_server_uri($pagelinkedto, 2048);
  454.  
  455.         if ($pingback_server_url) {
  456.                         set_time_limit( 60 ); 
  457.              // Now, the RPC call
  458.             debug_fwrite($log, "Page Linked To: $pagelinkedto \n");
  459.             debug_fwrite($log, 'Page Linked From: ');
  460.             $pagelinkedfrom = get_permalink($post_ID);
  461.             debug_fwrite($log, $pagelinkedfrom."\n");
  462.  
  463.             // using a timeout of 3 seconds should be enough to cover slow servers
  464.             $client = new IXR_Client($pingback_server_url);
  465.             $client->timeout = 3;
  466.             $client->useragent .= ' -- WordPress/' . $wp_version;
  467.  
  468.             // when set to true, this outputs debug messages by itself
  469.             $client->debug = false;
  470.             
  471.             if ( $client->query('pingback.ping', array($pagelinkedfrom, $pagelinkedto) ) )
  472.                 add_ping( $post_ID, $pagelinkedto );
  473.             else
  474.                 debug_fwrite($log, "Error.\n Fault code: ".$client->getErrorCode()." : ".$client->getErrorMessage()."\n");
  475.         }
  476.     }
  477.  
  478.     debug_fwrite($log, "\nEND: ".time()."\n****************************\n");
  479.     debug_fclose($log);
  480. }
  481.  
  482. function discover_pingback_server_uri($url, $timeout_bytes = 2048) {
  483.     global $wp_version;
  484.  
  485.     $byte_count = 0;
  486.     $contents = '';
  487.     $headers = '';
  488.     $pingback_str_dquote = 'rel="pingback"';
  489.     $pingback_str_squote = 'rel=\'pingback\'';
  490.     $x_pingback_str = 'x-pingback: ';
  491.     $pingback_href_original_pos = 27;
  492.  
  493.     extract(parse_url($url));
  494.  
  495.     if (!isset($host)) {
  496.         // Not an URL. This should never happen.
  497.         return false;
  498.     }
  499.  
  500.     $path  = (!isset($path)) ? '/'        : $path;
  501.     $path .= (isset($query)) ? '?'.$query : '';
  502.     $port  = (isset($port))  ? $port      : 80;
  503.  
  504.     // Try to connect to the server at $host
  505.     $fp = @fsockopen($host, $port, $errno, $errstr, 2);
  506.     if (!$fp) {
  507.         // Couldn't open a connection to $host;
  508.         return false;
  509.     }
  510.  
  511.     // Send the GET request
  512.     $request = "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: WordPress/$wp_version PHP/" . phpversion() . "\r\n\r\n";
  513.     ob_end_flush();
  514.     fputs($fp, $request);
  515.  
  516.     // Let's check for an X-Pingback header first
  517.     while (!feof($fp)) {
  518.         $line = fgets($fp, 512);
  519.         if (trim($line) == '') {
  520.             break;
  521.         }
  522.         $headers .= trim($line)."\n";
  523.         $x_pingback_header_offset = strpos(strtolower($headers), $x_pingback_str);
  524.         if ($x_pingback_header_offset) {
  525.             // We got it!
  526.             preg_match('#x-pingback: (.+)#is', $headers, $matches);
  527.             $pingback_server_url = trim($matches[1]);
  528.             return $pingback_server_url;
  529.         }
  530.         if(strpos(strtolower($headers), 'content-type: ')) {
  531.             preg_match('#content-type: (.+)#is', $headers, $matches);
  532.             $content_type = trim($matches[1]);
  533.         }
  534.     }
  535.  
  536.     if (preg_match('#(image|audio|video|model)/#is', $content_type)) {
  537.         // Not an (x)html, sgml, or xml page, no use going further
  538.         return false;
  539.     }
  540.  
  541.     while (!feof($fp)) {
  542.         $line = fgets($fp, 1024);
  543.         $contents .= trim($line);
  544.         $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
  545.         $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
  546.         if ($pingback_link_offset_dquote || $pingback_link_offset_squote) {
  547.             $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
  548.             $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
  549.             $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
  550.             $pingback_href_start = $pingback_href_pos+6;
  551.             $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
  552.             $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
  553.             $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
  554.             // We may find rel="pingback" but an incomplete pingback URI
  555.             if ($pingback_server_url_len > 0) {
  556.                 // We got it!
  557.                 return $pingback_server_url;
  558.             }
  559.         }
  560.         $byte_count += strlen($line);
  561.         if ($byte_count > $timeout_bytes) {
  562.             // It's no use going further, there probably isn't any pingback
  563.             // server to find in this file. (Prevents loading large files.)
  564.             return false;
  565.         }
  566.     }
  567.  
  568.     // We didn't find anything.
  569.     return false;
  570. }
  571.  
  572.  
  573. function wp_set_comment_status($comment_id, $comment_status) {
  574.     global $wpdb;
  575.  
  576.     switch($comment_status) {
  577.         case 'hold':
  578.             $query = "UPDATE $wpdb->comments SET comment_approved='0' WHERE comment_ID='$comment_id' LIMIT 1";
  579.         break;
  580.         case 'approve':
  581.             $query = "UPDATE $wpdb->comments SET comment_approved='1' WHERE comment_ID='$comment_id' LIMIT 1";
  582.         break;
  583.          case 'spam':
  584.              $query = "UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID='$comment_id' LIMIT 1";
  585.          break;
  586.         case 'delete':
  587.             $query = "DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1";
  588.         break;
  589.         default:
  590.             return false;
  591.     }
  592.     
  593.     if ($wpdb->query($query)) {
  594.         do_action('wp_set_comment_status', $comment_id, $comment_status);
  595.         return true;
  596.     } else {
  597.         return false;
  598.     }
  599. }
  600.  
  601.  
  602. function wp_get_comment_status($comment_id) {
  603.     global $wpdb;
  604.     
  605.     $result = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
  606.     if ($result == NULL) {
  607.         return 'deleted';
  608.     } else if ($result == '1') {
  609.         return 'approved';
  610.     } else if ($result == '0') {
  611.         return 'unapproved';
  612.     } else if ($result == 'spam') {
  613.         return 'spam';
  614.     } else {
  615.         return false;
  616.     }
  617. }
  618.  
  619. function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) {
  620.     global $wpdb;
  621.  
  622.     if (1 == get_settings('comment_moderation')) return false; // If moderation is set to manual
  623.  
  624.     if ( (count(explode('http:', $comment)) - 1) >= get_settings('comment_max_links') )
  625.         return false; // Check # of external links
  626.  
  627.     $mod_keys = trim( get_settings('moderation_keys') );
  628.     if ( !empty($mod_keys) ) {
  629.         $words = explode("\n", $mod_keys );
  630.  
  631.         foreach ($words as $word) {
  632.             $word = trim($word);
  633.  
  634.             // Skip empty lines
  635.             if (empty($word)) { continue; }
  636.  
  637.             // Do some escaping magic so that '#' chars in the 
  638.             // spam words don't break things:
  639.             $word = preg_quote($word, '#');
  640.         
  641.             $pattern = "#$word#i"; 
  642.             if ( preg_match($pattern, $author) ) return false;
  643.             if ( preg_match($pattern, $email) ) return false;
  644.             if ( preg_match($pattern, $url) ) return false;
  645.             if ( preg_match($pattern, $comment) ) return false;
  646.             if ( preg_match($pattern, $user_ip) ) return false;
  647.             if ( preg_match($pattern, $user_agent) ) return false;
  648.         }
  649.     }
  650.  
  651.     // Comment whitelisting:
  652.     if ( 1 == get_settings('comment_whitelist')) {
  653.         if ( 'trackback' == $comment_type || 'pingback' == $comment_type ) { // check if domain is in blogroll
  654.             $uri = parse_url($url);
  655.             $domain = $uri['host'];
  656.             $uri = parse_url( get_option('home') );
  657.             $home_domain = $uri['host'];
  658.             if ( $wpdb->get_var("SELECT link_id FROM $wpdb->links WHERE link_url LIKE ('%$domain%') LIMIT 1") || $domain == $home_domain )
  659.                 return true;
  660.             else
  661.                 return false;
  662.         } elseif( $author != '' && $email != '' ) {
  663.             $ok_to_comment = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_author = '$author' AND comment_author_email = '$email' and comment_approved = '1' LIMIT 1");
  664.             if ( 1 == $ok_to_comment && false === strpos( $email, get_settings('moderation_keys')) )
  665.                 return true;
  666.             else
  667.                 return false;
  668.         } else {
  669.             return false;
  670.         }
  671.     }
  672.  
  673.     return true;
  674. }
  675.  
  676. ?>
  677.